<?php

namespace addons\qingdongams\model;

use think\Db;
use think\Exception;
use think\Model;
use traits\model\SoftDelete;
use function EasyWeChat\Kernel\Support\get_client_ip;

/**
 *评论表
 */
class Comment Extends Model {
	use SoftDelete;

    // 表名,不含前缀
    const   SIGN_TYPE = 'sign';//签到
    const   CUSTOMER_TYPE    = 'customer';//客户
    const   EVENT_TYPE    = 'event';//日程
    const   ASSIST_EVENT_TYPE    = 'assist_event';//日程
    const   ARTICLE_TYPE    = 'article';// 文章评论
    const   WORKORDER_TYPE    = 'workorder';// 工单评论
    const   DISCUSS_TYPE    = 'discuss';// 工单评论
    const   WORKREPORT_TYPE    = 'workreport';// 工单评论

    const   DAILY_TYPE = 5;//工作报告
    // 表名,不含前缀
    protected $name = 'qingdongams_comment';
	// 开启自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';
	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';
	protected $deleteTime = 'deletetime';


	public function staff() {
		return $this->belongsTo(Staff::class, 'staff_id', 'id')->field('id,name,img,post');
	}

    public function getFileIdsAttr($value) {
        $files  = explode(',', $value);
        $result = [];
        foreach ($files as $fid) {
            if ($fid) {
                $result[] = cdnurl(File::getUrl($fid), true);
            }
        }

        return $result;
    }


    public function getCreatetimeAttr($value) {
		return date('Y-m-d H:i', $value);
	}
    //跟进
    public function record() {
        return $this->hasOne(Record::class, 'id', 'relation_id')->field('id,content');
    }
    //工作报告
    public function daily() {
        return $this->hasOne(Daily::class, 'id', 'relation_id')->field('id,type');
    }

    //添加评论
    public static function addComment($content,$files,$record_id,$relation_type,$staff_id)
    {
        $data = [
            'relation_type' => $relation_type,
            'relation_id' => $record_id,
            'staff_id' => $staff_id,
            'content' => $content,
            'file_ids' => $files,
            'status' => 1,
            'ip' => get_client_ip(),
        ];
        $staff = Staff::info();
        $commentModel = new self();
        Db::startTrans();
        try {
            $commentModel->save($data);
            if ($relation_type ==$commentModel::SIGN_TYPE) {
                $record = StaffSignIn::where(['id' => $record_id])->find();
                $record->create_staff_id=$record['staff_id'];
                $type = Message::RECORD_TYPE;
                $content = $staff->name . '评论了您提交的跟进记录!';
                $content2 = $staff->name . '评论了您关注的跟进记录!';
            } elseif ($relation_type ==$commentModel::EVENT_TYPE) {
                $record = Event::where(['id' => $record_id])->find();
                $type = Message::EVENT_TYPE;
                $content = $staff->name . '评论了您的日程!';
                $content2 = $staff->name . '评论了您的日程!';
            } elseif ($relation_type ==$commentModel::ASSIST_EVENT_TYPE) {
                $record = Event::where(['id' => $record_id])->find();
                $type = Message::ASSIST_EVENT_TYPE;
                $content = $staff->name . '评论了您的协助任务!';
                $content2 = $staff->name . '评论了您的协助任务!';
            } elseif($relation_type ==$commentModel::ARTICLE_TYPE) {
                Aftermarket::where(['id'=>$record_id])->setInc('comment_num');
                $record = Aftermarket::where(['id' => $record_id])->find();
                $type = Message::ARTICLE_TYPE;
                $content = $staff->name . '增加了文章评论!';
                $content2 = $staff->name . '增加了文章评论!';
            }elseif($relation_type ==$commentModel::WORKREPORT_TYPE) {
                $record = WorkReport::where(['id' => $record_id])->find();
                $type = Message::WORKREPORT_TYPE;
                $content = $staff->name . '评论了您的工作报告!';
                $content2 = $staff->name . '评论了您关注的工作报告!';
                $record['create_staff_id']=$record['staff_id'];
            }elseif($relation_type ==$commentModel::WORKORDER_TYPE) {
                $record = \addons\qingdongams\model\Workorder::where(['id' => $record_id])->find();
                $type = Message::WORKORDER_TYPE;
                $content = $staff->name . '评论了您的工单!';
                $content2 = $staff->name . '评论了您关注的工单!';
            }elseif($relation_type ==$commentModel::DISCUSS_TYPE) {
                $record = \addons\qingdongams\model\Discuss::where(['id' => $record_id])->find();
                $type = Message::DISCUSS_TYPE;
                $content = $staff->name . '评论了您的讨论!';
                $content2 = $staff->name . '评论了您关注的讨论!';
            }
            if(empty($record)){
                throw new Exception('记录不存在');
            }
            if ($relation_type ==$commentModel::ASSIST_EVENT_TYPE) {
                Message::addMessage($type, $record_id, $record['create_staff_id'], $staff->id, $content);
                Message::addMessage($type, $record_id, $record['staff_id'], $staff->id, $content2);
            }else{
                Message::addMessage($type, $record_id, $record['create_staff_id'], $staff->id, $content);
                $staff_ids = $commentModel->where(['relation_type' => $relation_type, 'relation_id' => $record_id])->group('staff_id')->column('staff_id');
                foreach ($staff_ids as $staff_id) {
                    //发送通知
                    if ($staff_id != $staff->id) {
                        Message::addMessage($type, $record_id, $staff_id, $staff->id, $content2);
                    }
                }
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            throw new Exception($e->getMessage());
        }
        return true;
    }
}
